﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.Entities;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.DataLayer;
namespace BaHuan
{
    public partial class FrmTaiKhoanNH : Form
    {
        private TaiKhoanGiaoDichManagement taikhoanMgt = new TaiKhoanGiaoDichManagement();
        private TaiKhoanGiaoDich taikhoan = new TaiKhoanGiaoDich();
        private NganHangManagement nganhangMgt = new NganHangManagement();
        private NganHang nganhang;
        private bool IsEdited = false;

        public NganHang NganHang
        {
            set { this.nganhang=value; }
            get { return this.nganhang; }
        }
        public FrmTaiKhoanNH()
        {
            InitializeComponent();
        }

        public void BindingToGrid()
        {
            ResultObj result = new ResultObj();

            result = taikhoanMgt.GetAllTaiKhoanGiaoDich();
             ResultObj result1 = new ResultObj();

            result = taikhoanMgt.GetAllTaiKhoanGiaoDich();
            List<TaiKhoanGiaoDich> listTaiKhoanGiaoDich = (from c in (List<TaiKhoanGiaoDich>)result.Result
                                                           where c.IdNganHang== this.nganhang.Id
                                                           select c).ToList();
            dataGridView1.DataSource = listTaiKhoanGiaoDich;
          
         

        }

        private void FrmTaiKhoanNH_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            groupTaiKhoanNH.Enabled = false;
           
            btnSua.Enabled = false;
            btnLuu.Enabled = false;
            
            BindingToGrid();

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            clearTextbox();
            txtIdNganHang.Text = nganhang.Id.ToString();
            txtIdNganHang.Enabled = false;
            groupTaiKhoanNH.Enabled = true;
            btnLuu.Enabled = true;
            txtTenTaiKhoan.Select();
        }
       
        public void ChangeStatusTextBox(bool IsReadOnly)
        {
            if (IsReadOnly)
            {
                txtMaTaiKhoan.ReadOnly = true;
                txtTenTaiKhoan.ReadOnly = true;
            }
            else
            {
                txtTenTaiKhoan.ReadOnly = false;
                txtMaTaiKhoan.ReadOnly = false;

            }
        }

        public void clearTextbox()
        {
            txtMaTaiKhoan.Clear();
            txtTenTaiKhoan.Clear();


        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            ChangeStatusTextBox(true);
            lblId.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            txtIdNganHang.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
            txtTenTaiKhoan.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            txtMaTaiKhoan.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            btnSua.Enabled = true;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = true;
            IsEdited = true;
            groupTaiKhoanNH.Enabled = true;
            ChangeStatusTextBox(false);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ResultObj result = new ResultObj();
            int nIdNganHang = this.nganhang.Id;
            string sIdNganHang = txtIdNganHang.Text;
            string sMaTaiKhoan = txtMaTaiKhoan.Text;
            string sTenTaiKhoan =txtTenTaiKhoan.Text;
            TaiKhoanGiaoDich taikhoangiaodich = new TaiKhoanGiaoDich();
            taikhoangiaodich.SoTaiKhoanGiaoDich = sMaTaiKhoan;
            taikhoangiaodich.TenGiaoDich = sTenTaiKhoan;
           
            taikhoangiaodich.IdNganHang = nIdNganHang;
           

            if (!IsEdited)
            {
                result = taikhoanMgt.SaveNewTaiKhoanGiaoDich(taikhoangiaodich);
            }
            else
            {
                taikhoan.Id = Convert.ToInt32(lblId.Text);
                taikhoan.IdNganHang = Convert.ToInt32(txtIdNganHang.Text);
                taikhoan.TenGiaoDich = txtTenTaiKhoan.Text;
                taikhoan.SoTaiKhoanGiaoDich = txtMaTaiKhoan.Text;
                result = taikhoanMgt.UpdateTaiKhoanGiaoDich(taikhoan); 
                IsEdited = true;
            }
            if (result.ReturnCode > 0)
                MessageBox.Show("Lưu Thành Công", "Thông báo");
            else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
            else
                MessageBox.Show("Thất bại");

            BindingToGrid();

        }
    }
}
